package com.zym.init.controller.backend;

import com.zym.init.core.annotation.ApiBackendRestController;
import com.zym.init.core.annotation.Log;
import com.zym.init.core.controller.BaseController;
import com.zym.init.core.domain.AjaxResult;
import com.zym.init.model.query.ResourceQuery;
import com.zym.init.model.query.RoleQuery;
import com.zym.init.service.ResourceService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@ApiBackendRestController
@RequestMapping(path = "/resource", produces = "application/json")
public class ResourceController extends BaseController {

    private final ResourceService resourceService;

    public ResourceController(ResourceService resourceService) {
        this.resourceService = resourceService;
    }

    /**
     * 获取列表
     */
    @Log(title = "获取资源列表")
    @PreAuthorize("@ss.hasPermission('system:resource:list')")
    @GetMapping
    public AjaxResult list(ResourceQuery query) {
        return success("获取成功", resourceService.getPage(query));
    }

    /**
     * 删除
     */
    @Log(title = "删除资源")
    @PreAuthorize("@ss.hasPermission('system:resource:delete')")
    @DeleteMapping
    public AjaxResult delete(@RequestParam("ids") List<Long> ids) {
        return resourceService.deleteBatchByIds(ids) ? success("删除成功") : fail("删除失败");
    }

    /**
     * 获取面包屑列表
     */
    @Log(title = "获取资源面包屑")
    @PreAuthorize("@ss.hasPermission('system:resource:list')")
    @GetMapping("/breadcrumb/{id}")
    public AjaxResult breadcrumb(@PathVariable("id") Long id) {
        return success(resourceService.getBreadcrumb(id));
    }

    /**
     * 编辑
     */
    @Log(title = "编辑资源")
    @PreAuthorize("@ss.hasPermission('system:resource:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(value = {RoleQuery.Update.class}) ResourceQuery query) {
        return resourceService.edit(query) ? success("编辑成功") : fail("编辑失败");
    }

}
